define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coupon/index' + location.search,
                    add_url: 'coupon/add',
                    edit_url: 'coupon/edit',
                    del_url: 'coupon/del',
                    multi_url: 'coupon/multi',
                    import_url: 'coupon/import',
                    table: 'coupon',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                // $("input[name='store_id']", form).addClass("selectpage").data("source", "store/ajaxgetstorewithquanoption").data("primaryKey", "value").data("field", "name");
                // Form.events.cxselect(form);
                // Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'coupon_name', title: __('Coupon_name'), operate: 'LIKE'},
                        // {field: 'coupon_type', title: __('Coupon_type'), searchList: {"1":__('Coupon_type 1'),"2":__('Coupon_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'reduce_price', title: __('Reduce_price'), operate:'BETWEEN', formatter: Controller.api.show_reduce_price},
                        // {field: 'discount', title: __('Discount'),  operate:'BETWEEN',  formatter: Controller.api.show_discount},
                        {field: 'min_price', title: __('Min_price'), operate:'BETWEEN'},
                        {field: 'expire_type', title: __('Expire_type'), searchList: {"1":__('Expire_type 1'),"2":__('Expire_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'expire_day', title: __('Expire_day'), operate: 'BETWEEN', formatter: Controller.api.show_expire_day},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Controller.api.show_start_end_day},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Controller.api.show_start_end_day},

                        // {field: 'store_id', title: __('Store_id'), operate: '=', visible: false},
                        // {field: 'store.store_name', title: __('Store_id'), operate: false, formatter: Controller.api.showstore},
                        // {field: 'room_id', title: __('Room_id'), operate: false, visible: false},
                        // {field: 'room.room_name', title: __('Room_id'), operate: false, formatter: Controller.api.showroom},

                        // {field: 'total_num', title: __('Total_num'), operate: 'BETWEEN', formatter: Controller.api.show_total_num},
                        {field: 'receive_num', title: __('Receive_num'), operate: 'BETWEEN'},
                        // {field: 'num_per_person', title: __('Num_per_person'), operate: 'BETWEEN', formatter: Controller.api.show_num_per_person},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"0":__('Status 0')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                search: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'coupon/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'coupon/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'coupon/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
            Controller.api.add_edit_init();
        },
        edit: function () {
            Controller.api.bindevent();
            Controller.api.add_edit_init();
        },
        api: {
            show_reduce_price: function (e, row) {
                if (1 == row.coupon_type) {
                    return e
                } else {
                    return '-'
                }
            },
            show_discount: function (e, row) {
                if (2 == row.coupon_type) {
                    return e / 10 + '折'
                } else {
                    return '-'
                }
            },
            show_expire_day: function (e, row) {
                if (1 == row.expire_type) {
                    return e
                } else {
                    return '-'
                }
            },
            show_start_end_day: function (e, row) {
                if (2 == row.expire_type) {
                    return e
                } else {
                    return '-'
                }
            },
            showstore: function (e, row) {
                if (0 == row.store_id) {
                    return '全部门店'
                }
                if (null == e) {
                    return '-'
                }
                return e
            },
            showroom: function (e, row) {
                if (0 == row.room_id) {
                    return '全部教室'
                }
                if (null == e) {
                    return '-'
                }
                return e
            },
            show_total_num: function (e) {
                if (0 == e) {
                    return '不限制'
                }
                return e
            },
            show_num_per_person: function (e) {
                if (0 == e) {
                    return '不限制'
                }
                return e
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            add_edit_init: function () {
                let coupontype = $('select[name="row[coupon_type]"]')
                let reduce_price_div = $('div.reduce_price_part')
                let discount_div = $('div.discount_part')
                if (1 == coupontype.val()) {
                    reduce_price_div.show()
                    discount_div.hide()
                } else if (2 == coupontype.val()) {
                    reduce_price_div.hide()
                    discount_div.show()
                }
                coupontype.change(function (e){
                    if (1 == $(this).val()) {
                        reduce_price_div.show()
                        discount_div.hide()
                    } else if (2 == $(this).val()) {
                        reduce_price_div.hide()
                        discount_div.show()
                    }
                })

                let expire_type = $('select[name="row[expire_type]"]')
                let expire_day_part = $('div.expire_day_part')
                let start_end_part = $('div.start_end_part')

                if (1 == expire_type.val()) {
                    expire_day_part.show()
                    start_end_part.hide()
                } else if (2 == expire_type.val()) {
                    expire_day_part.hide()
                    start_end_part.show()
                }
                expire_type.change(function (e){
                    if (1 == $(this).val()) {
                        expire_day_part.show()
                        start_end_part.hide()
                    } else if (2 == $(this).val()) {
                        expire_day_part.hide()
                        start_end_part.show()
                    }
                })

                $.validator.config({
                    rules: {
                        reduce_price: function (element) {
                            if (1 == coupontype.val()) {
                                if ($(element).val() > 0) {
                                } else {
                                    return '请输入大于0的数字'
                                }
                            }
                        },
                        discount: function (element) {
                            if (2 == coupontype.val()) {
                                if ($(element).val() > 0 && $(element).val() < 100) {
                                } else {
                                    return '请输入1-100的数字'
                                }
                            }
                        },
                        expire_day_need_required: function (element) {
                            return  1 == expire_type.val()
                        },
                        expire_day: function (element) {
                            console.log(expire_type.val())
                            if (1 == expire_type.val()) {
                                if ($(element).val() > 0) {
                                } else {
                                    return '请输入大于0的数字'
                                }
                            }
                        },
                        start_end_time_need_required: function (element) {
                            return  2 == expire_type.val()
                        },
                    }
                });
            }

        }
    };
    return Controller;
});
